/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid.util;


/**
 * <p>Filter for numbers</p>
 * <p><a href="NumberFilter.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.5 $ $Date: 2006/08/03 10:08:22 $
 */
public class NumberFilter extends BaseFilter {


	// ~ Instance variables

	private String firstCondition = FilterConditions.NUMBER_EQUAL;
	private Float firstValue = new Float(0);
	private String secondCondition = FilterConditions.CONDITION_NOT_SET;
	private Float secondValue = null;
	private String logicCopula = FilterConditions.LOGIC_COPULA_AND;

	/**
	 * Creates new instance of NumberFilter
	 *
	 * @param fieldName Name of field to associate filter with
	 */
	public NumberFilter(String fieldName) {
		super(fieldName);
	}

	/**
	 * Creates clause from specified condition and float to compare our field to
	 *
	 * @param condition Condition to insert into clause
	 * @param compareTo Float to compare to
	 * @return Constructed clause
	 */
	private String createClause(String condition, Float compareTo) {
		if ( condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
			return createNullComparison(condition);
		} else {
			return fieldName + " " + condition + " " + compareTo.toString();
		}
	}

	/**
	 * Creates first clause
	 *
	 * @param condition Congruence condition
	 * @param compareTo Float to compare to
	 */
	public void createFirstClause(String condition, Float compareTo) {
		this.firstCondition = condition;
		this.firstValue = compareTo;
		setFirstClause(createClause(condition, compareTo));
	}

	/**
	 * Creates second clause
	 *
	 * @param condition   Congruence condition
	 * @param compareTo   Float to compare to
	 * @param logicCopula Logic copula to place between first and second conditions
	 */
	public void createSecondClause(String condition, Float compareTo, String logicCopula) {
		this.secondCondition = condition;
		this.secondValue = compareTo;
		this.logicCopula = logicCopula;
		setSecondClause(createClause(condition, compareTo), logicCopula);
	}

	/**
	 * Creates second clause with AND logic copula
	 *
	 * @param condition Congruence condition
	 * @param compareTo Float to compare to
	 */
	public void createSecondClause(String condition, Float compareTo) {
		createSecondClause(condition, compareTo, FilterConditions.LOGIC_COPULA_AND);
	}

    /**
     * Returns first condition
     *
     * @return first condition
     */
	public String getFirstCondition() {
		return firstCondition;
	}

    /**
     * Returns first value
     *
     * @return first value
     */
	public Float getFirstValue() {
		return firstValue;
	}

    /**
     * Returns second condition
     *
     * @return second condition
     */
	public String getSecondCondition() {
		return secondCondition;
	}

    /**
     * Returns second value
     *
     * @return second value
     */
	public Float getSecondValue() {
		return secondValue;
	}

    /**
     * Returns logic copula
     *
     * @return logic copula
     */
	public String getLogicCopula() {
		return logicCopula;
	}

}
